package com.rongxun.hbwallet.ui.activities;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.BaseActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.Md5Utils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.view.InputPwdDialog;
import com.rongxun.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/21/021.
 * e-mail:lomapa@163.com
 */

public class WalletBackupActivity extends BaseActivity {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_title)
    TextView  mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.et_wallet_pwd)
    EditText  mEtWalletPwd;
    @BindView(R.id.btn_backup)
    TextView  mBtnBackup;
    private InputPwdDialog inputPwdDialog;
    private String         walletPwd;
    private String         walletAddress;
    private String         walletName;
    private String         walletMnemonic;
    private long           walletId;
    private Dialog         loadingDialog;
    private ETHWallet      mEthWallet;


    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.wallet_backup_backup_btn));
        mToolbarBack.setVisibility(View.VISIBLE);
        loadingDialog = LoadingDialog.createLoadingDialog(this, getString(R.string.load), false);
        Intent intent = getIntent();
        mEthWallet = intent.getParcelableExtra(IntentKey.ETH_WALLET);

        walletId = mEthWallet.getId();
        walletPwd = mEthWallet.getPassword();
        walletAddress = mEthWallet.getAddress();
        walletName = mEthWallet.getName();
        walletMnemonic = mEthWallet.getMnemonic();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_wallet_backup;
    }


    @OnClick({R.id.toolbar_back, R.id.btn_backup})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_backup:
                if (TextUtils.isEmpty(mEtWalletPwd.getText().toString())) {
                    ToastUtils.showToast(R.string.input_pwd_dialog_tip);
                } else if (TextUtils.equals(Md5Utils.md5(mEtWalletPwd.getText().toString()), walletPwd)) {
                    ActivityUtils.startActivityWithAnimotion(this,
                            new Intent(this, MnemonicBackupActivity.class)
                                    .putExtra(IntentKey.ETH_WALLET, mEthWallet));
                    finish();
                }else{
                    ToastUtils.showToast(R.string.wallet_detail_wrong_pwd);
                }
                break;
            case R.id.toolbar_back:
                onBackPressed();
                break;
        }
    }

}
